#include "NtupleLoader.C"
#include <iostream>
#include <sstream>

/*
 btag = true : with btagging cut on event selection
 btag = false: without btagging cut on event selection

 tria = true : with triangle cut on event selection
 tria = false: without triangle cut on event selection

 test = true : using data10 test sample (periodG)
 test = false: using all data10 data 
*/

int run_analysis(const bool btag=false, const bool tria=false, const float d0sig_t=5.0, const bool test=false, const Int_t cache_size=10000000) {

   // TSelector object
   //TSelector * s = TSelector::GetSelector("EventSelector.C");

   // making up selector options
   ostringstream stm;
   stm << "d0sig=" << d0sig_t;
   if (btag) stm << " cbtagging";
   if (tria) stm << " ctriangle";

   TString data_opts = TString( stm.str() ) + " hscale=1.0";

   TChain* myChain;

   TString proofDataset = "data10_local";

   Double_t scale = 1.0;
   if (test) {
       scale  = getIntLumi("data10_test");
       //proofDataset = "data10_test";
       proofDataset = "data10_test_local";
   }
   else {
       scale  = getIntLumi("data10");
   }

   stm << " hscale=" << scale;
   TString mc_opts = TString( stm.str() );

   cout << data_opts << endl;
   cout << mc_opts << endl;

   // start up proof (get proof cluster instance)
   TProof *p = TProof::Open(gSystem->GetFromPipe("pod-info -c"));

   // presuming the given proof dataset is registered and verified in advance
   p->SetDataSetTreeName(proofDataset, "tree");

   p->ShowDataSet(proofDataset, "");

   // this is only for workers running on the same machine 
   //p->AddIncludePath("/localstore/hclee/netbeans_projects/lhc-atlas-handy-tools/study/qcd", kTRUE);
   //p->Load("PlotQCD.C");
   //p->UploadPackage("proof/EventSelector.par");
   p->EnablePackage("EventSelector");

   // data sample
   p->Process(proofDataset, "EventSelector.C", data_opts + " sample=data10" );

   // MC sample 
   p->Process("mc09_ttbar"  , "EventSelector.C", mc_opts + " sample=mc_ttbar"   );
   p->Process("mc09_stop"   , "EventSelector.C", mc_opts + " sample=mc_stop"    );
   p->Process("mc09_wjets"  , "EventSelector.C", mc_opts + " sample=mc_wjets"   );
   p->Process("mc09_zjets"  , "EventSelector.C", mc_opts + " sample=mc_zjets"   );
   p->Process("mc09_diboson", "EventSelector.C", mc_opts + " sample=mc_diboson" );
   p->Process("mc09_qcd"    , "EventSelector.C", mc_opts + " sample=mc_qcd"     );
   p->Process("mc09_mix"    , "EventSelector.C", mc_opts + " sample=mc_mix"     );

   return 0;
}
